package com.enation.app.web.core.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.enation.app.web.core.model.Member;
import com.enation.app.web.core.service.IMemberManager;
import com.enation.framework.context.webcontext.ThreadContextHolder;
import com.enation.framework.database.IDaoSupport;
import com.enation.framework.database.Page;
import com.enation.framework.util.DateUtil;
import com.enation.framework.util.StringUtil;

/**
 * 
 * @function 成员管理
 * @author 闯哥
 * @time 2016年11月30日 下午11:26:01
 */
@Service
public class MemberManager implements IMemberManager {

	@Autowired
	private IDaoSupport daoSupport;

	@Override
	public void registor(Member member) {

		this.daoSupport.insert("es_member", member);

	}

	@Override
	public int checkIsRegister(String student_number) {

		String sql = "select count(*) from es_member where student_number=?";
		int count = this.daoSupport.queryForInt(sql, student_number);

		if (count > 0) {
			count = 1;
		} else {
			count = 0;
		}

		return count;
	}

	@Override
	public void logincountAndlast(Integer member_id, String logincount,
			Long lastlogin) {

		String sql = "update es_member set logincount=?,lastlogin=? where member_id=?";
		this.daoSupport.execute(sql, logincount, lastlogin, member_id);

	}

	@Override
	public Member findByStudentNumber(String student_number) {

		String sql = "select * from es_member where student_number=?";
		Member member = (Member) this.daoSupport.queryForObject(sql,
				Member.class, student_number);
		return member;
	}

	@Override
	public int checkNickName(String nick_name) {

		String sql = "select count(*) from es_member where nick_name=?";
		int count = this.daoSupport.queryForInt(sql, nick_name);

		if (count > 0) {
			return 1;
		}

		return 0;
	}

	@Override
	public Page findByPage(int pageNo, int pageSize, Map<String, String> result) {

		StringBuffer sql = new StringBuffer(
				"select * from es_member where disabled=0 and is_activation=1");

		String student_number = result.get("student_number");
		if (student_number != null) {
			if (!student_number.equals("")) {
				student_number = student_number.toUpperCase();
				sql.append(" and student_number like '%" + student_number
						+ "%'");
			}
		}

		String phone = result.get("phone");
		if (phone != null) {
			if (!phone.equals("")) {
				sql.append(" and phone like '%" + phone + "%'");
			}
		}

		String start_time = result.get("start_time");
		if (start_time != null) {
			if (!start_time.equals("")) {
				Date start_date = DateUtil.toDate(start_time, "yyyy-MM-dd");
				Long start = start_date.getTime() / 1000;
				sql.append(" and create_time>" + start);
			}
		}

		String end_time = result.get("end_time");
		if (end_time != null) {
			if (!end_time.equals("")) {
				Date end_date = DateUtil.toDate(end_time, "yyyy-MM-dd");
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(end_date);
				calendar.add(Calendar.DAY_OF_YEAR, 1);
				end_date = calendar.getTime();
				Long end = end_date.getTime() / 1000;
				sql.append(" and create_time<" + end);
			}
		}

		String real_name = result.get("real_name");
		if (real_name != null) {
			if (!real_name.equals("")) {
				sql.append(" and real_name like '%" + real_name + "%'");
			}
		}

		String nick_name = result.get("nick_name");
		if (nick_name != null) {
			if (!nick_name.equals("")) {
				sql.append(" and nick_name like '%" + nick_name + "%'");
			}
		}

		String grade = result.get("grade");
		if (grade != null) {
			if (!grade.equals("")) {
				sql.append(" and grade like '%" + grade + "%'");
			}
		}

		String sex = result.get("sex");
		if (sex != null) {
			Integer sexint = Integer.parseInt(sex);
			if (sexint != 2) {
				sql.append(" and sex=" + sexint);
			}
		}

		String strprovince_id = result.get("province_id");
		if (strprovince_id != null) {
			if (!strprovince_id.equals("") && !strprovince_id.equals("0")) {
				Integer province_id = Integer.parseInt(strprovince_id);
				sql.append(" and province_id=" + province_id);

			}
		}

		String strCity_id = result.get("city_id");
		if (strCity_id != null) {
			if (!strCity_id.equals("") && !strCity_id.equals("0")) {
				Integer city_id = Integer.parseInt(strCity_id);
				sql.append(" and city_id=" + city_id);

			}
		}

		String strRegion_id = result.get("region_id");
		if (strRegion_id != null) {
			if (!strRegion_id.equals("") && !strRegion_id.equals("0")) {
				Integer region_id = Integer.parseInt(strRegion_id);
				sql.append(" and region_id=" + region_id);

			}
		}

		sql.append(" order by member_id desc");

		Page page = this.daoSupport.queryForPage(sql.toString(), pageNo,
				pageSize, Member.class);
		return page;
	}

	@Override
	public void delete(String member_ids) {

		String sql = "delete from es_member where member_id in (" + member_ids
				+ ")";
		this.daoSupport.execute(sql);

	}

	@Override
	public Member getCurrentLogin() {

		HttpServletRequest request = ThreadContextHolder.getHttpRequest();
		HttpSession session = request.getSession();

		Member member = (Member) session.getAttribute("member");

		return member;
	}

	@Override
	public Member findById(Integer member_id) {

		String sql = "select * from es_member where member_id=?";
		Member member = (Member) this.daoSupport.queryForObject(sql,
				Member.class, member_id);
		return member;
	}

	@Override
	public void update(Member member) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("member_id", member.getMember_id());
		this.daoSupport.update("es_member", member, map);
	}

	@Override
	public Member findByStudentNumberAndPassword(String student_number,
			String password) {

		String sql = "select * from es_member where student_number=? and password=?";
		Member member = (Member) this.daoSupport.queryForObject(sql,
				Member.class, student_number, StringUtil.md5(password));
		return member;
	}

}
